﻿using AntdUI;
using BST250301.Models;
using BST250301TestStand.Models;
using BST250301TestStand.Models.Utils;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Mysqlx.Crud;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace BST250301.Views
{

    public partial class ProjectTestGrok : UserControl
    {

        private AntdUI.Window window;
        AntList<ProjectItem> antList = new AntList<ProjectItem>();
        int orderid = 1;
        public ProjectTestGrok()
        {

            InitData();
        }


        private async Task InitData()
        {
            // Setup Host and DI
            var host = Host.CreateDefaultBuilder()
                .ConfigureServices((context, services) =>
                {
                    var connectionString =MainWindow.configapp.DBConnectionServer;
                    //"Server=81.70.27.42;Database=transmissiontest;Port=8306;Uid=sa;Pwd=W1717@sina.com";
                    services.AddDbContext<AppDbContext>(options =>
                        options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
                })
                .Build();

            using var scope = host.Services.CreateScope();
            var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();

            // Migrate database (create if not exists)
           // await dbContext.Database.MigrateAsync();

            // Example: Add a user
            var user = new User { Name = "admin" };
            //dbContext.Users.Add(user);
            //await dbContext.SaveChangesAsync();

            // Example: Add a parameter
            var param = new Parameter { Name = "wxj", DataType = "Float", DefaultValue = "100.0" };
            dbContext.Parameters.Add(param);
            await dbContext.SaveChangesAsync();

            // Example: Link parameter to project (assume Project Id 1 exists)
            var projectParam = new ProjectParameter
            {
                ProjectId = 1,
                ParameterId = param.Id,
                IsAdjustable = true
            };
            dbContext.ProjectParameters.Add(projectParam);
            await dbContext.SaveChangesAsync();

            // Example: Create a template for user
            var template = new Template { Name = "TestTemplate", UserId = user.Id };
            dbContext.Templates.Add(template);
            await dbContext.SaveChangesAsync();

            // Add project to template (repeatable)
            var templateProject1 = new TemplateProject { TemplateId = template.Id, ProjectId = 1, SequenceOrder = 1 };
            var templateProject2 = new TemplateProject { TemplateId = template.Id, ProjectId = 1, SequenceOrder = 2 };  // Repeat
            dbContext.TemplateProjects.AddRange(templateProject1, templateProject2);
            await dbContext.SaveChangesAsync();

            // Add adjusted parameter to template project
            var templateParam = new TemplateProjectParameter
            {
                TemplateProjectId = templateProject1.Id,
                ParameterId = param.Id,
                Value = "150.0"  // Adjusted value
            };
            dbContext.TemplateProjectParameters.Add(templateParam);
            await dbContext.SaveChangesAsync();

            // Query example: Load template with projects and params
            var loadedTemplate = await dbContext.Templates
                .Include(t => t.TemplateProjects)
                    .ThenInclude(tp => tp.TemplateProjectParameters)
                .FirstOrDefaultAsync(t => t.Id == template.Id);

            Console.WriteLine($"Template: {loadedTemplate?.Name}");
            foreach (var tp in loadedTemplate?.TemplateProjects ?? Enumerable.Empty<TemplateProject>())
            {
                Console.WriteLine($"- Project {tp.ProjectId} (Order: {tp.SequenceOrder})");
                foreach (var tpp in tp.TemplateProjectParameters)
                {
                    Console.WriteLine($"  - Param {tpp.ParameterId}: {tpp.Value}");
                }
            }
        }


        private void btnConfim_Click(object sender, EventArgs e)
        {

        }

        private void buttonAddPro_Click(object sender, EventArgs e)
        {

        }
       
    }
}
